package com.dd.census.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dd.census.emums.DictionaryGroupEnum;
import com.dd.census.entity.Dictionary;
import com.dd.census.mapper.DictionaryMapper;
import com.dd.census.service.DictionaryService;

@Service
public class DictionaryServiceImpl  implements DictionaryService{
	
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	public Map<String,Object> selectPage(Dictionary dictionary){
		Map<String,Object> map = new HashMap<String,Object>();
		int total = dictionaryMapper.selectCount(dictionary);
		map.put("pageCount", Math.ceil((double)total/dictionary.getPageSize()));//总页数
		map.put("total", total);//总条数
		map.put("index", dictionary.getPageIndex());//当前页
		if(total>0) {
			dictionary.setPageIndex(dictionary.getPageSize()*(dictionary.getPageIndex()-1));
			List<Dictionary> list =dictionaryMapper.selectPage(dictionary);
			for (Dictionary dictionary2 : list) {
				dictionary2.setGroupCode(DictionaryGroupEnum.getValue(dictionary2.getGroupName()).toString());
			}
			map.put("items", list);
		}
		return map;
	}
	
	public List<Dictionary> selectAll(Dictionary dictionary){
		return dictionaryMapper.selectAll(dictionary);
	}
	
	public List<Dictionary> selectCode(String groupName){
		return dictionaryMapper.selectCode(groupName);
	}

	public int insert(Dictionary dictionary) {
		dictionaryMapper.insert(dictionary);
		return dictionary.getId();
	}
	
	public int update(Dictionary dictionary) {
		return dictionaryMapper.update(dictionary);
	}
	
	public Dictionary selectById(Integer id) {
		return dictionaryMapper.selectById(id);
	}

	@Override
	public int delete(Dictionary dictionary) {
		return dictionaryMapper.delete(dictionary);
	}
}